package com.fanbai.score.business.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fanbai.score.R;
import com.fanbai.score.business.beans.ShopInfo_Bean;
import com.fanbai.score.business.fragemnt.Home_Fragment;
import com.fanbai.score.business.http.Busin_Goods_RequestNet;
import com.fanbai.score.business.http.Busin_User_RequestNet;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.activity.WebView_Activity;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.bean.HeadImage_Bean;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by acer on 2017/6/17.
 */

public class ShopList_Activity extends BaseActivity{

    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.Tenant_rollPagerView)
    RollPagerView mRollViewPager;
    List<HeadImage_Bean> headImageBeanList;//轮播图数据
    @Override
    protected int getLayoutID() {
        return R.layout.business_activity_shoplist;
    }

    @Override
    public String getTitleName() {
        return "商铺列表";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        getshopinfos();
        getHeadImages();
    }

    private void setData(final List<ShopInfo_Bean> beans){
        List<String> dataList = new ArrayList<>();
        dataList.add("htsec01.png");
        dataList.add("htsec01.png");
        dataList.add("htsec01.png");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
        CommonAdapter adapter = new CommonAdapter<ShopInfo_Bean>(ctx, R.layout.business_item_activity_shoplist,
                beans){
            @Override
            protected void convert(final ViewHolder holder, ShopInfo_Bean bean, int position) {
                holder.setText(R.id.businessItemActivityShopList_title, bean.getSname());
                holder.setText(R.id.businessItemActivityShopList_add, bean.getSdetailaddress());
                holder.setText(R.id.businessItemActivityShopList_totle, "今日销售"+bean.getTotalprice()+"元");
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("sid", beans.get(position).getSid());
                jumpToActivity(StaffList_Activity.class, bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        mRollViewPager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (headImageBeanList.size() > position) {
                    HeadImage_Bean bean = headImageBeanList.get(position);
                    if (bean.getUrl() != null && !bean.getUrl().equals("")) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", "" + bean.getUrl());
                        ctx.jumpToActivity(WebView_Activity.class, bundle);
                    }
                }
            }
        });
    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishActivity();
    }

    private void getshopinfos(){
        showDialog();
        Busin_Goods_RequestNet.getshopinfos(ctx, new CallBack<List<ShopInfo_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<ShopInfo_Bean> beans) {
                hideDialog();
                setData(beans);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
            }
        });
    }

    private void getHeadImages(){
        Busin_User_RequestNet.getHeadImages2(ctx, new CallBack<List<HeadImage_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<HeadImage_Bean> beans) {
                headImageBeanList = beans;
                //2个Adapter用法一样;
                TestNomalAdapter mAdapter = new TestNomalAdapter(headImageBeanList);
                mRollViewPager.setAdapter(mAdapter);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private class TestNomalAdapter extends StaticPagerAdapter {
        List<HeadImage_Bean> headImage_been;

        public TestNomalAdapter(List<HeadImage_Bean> headImage_been) {
            this.headImage_been = headImage_been;
        }

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            ImageUtils.show(ctx, headImage_been.get(position).getImage(),
                    view, R.mipmap.icon_img_defeated);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }

        @Override
        public int getCount() {
            return headImage_been.size();
        }
    }
}
